package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page page = (Page) categoryMapper.page(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category = new Category();
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateTime(LocalDateTime.now());
        category.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.insert(category);
        BaseContext.removeCurrentId();
    }

    @Override
    public void status(Integer status, Long id) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
        categoryMapper.status(category);
        BaseContext.removeCurrentId();
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.status(category);
        BaseContext.removeCurrentId();
    }

    @Override
    public void delete(Long id) {
        Integer count = dishMapper.getByid(id);
        if (count  > 0 ) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
         count = setmealMapper.getByid(id);
        if (count > 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        Category category = categoryMapper.byid(id);
        if (category.getStatus() == StatusConstant.ENABLE) {
            throw new BaseException(MessageConstant.CATEGOY_ON_SALE);
        }
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> category = categoryMapper.list(type);
        return category;
    }
}
